<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单流水号" prop="no">
        <el-input v-model="formData.no"  disabled/>
      </el-form-item>
      <el-form-item label="医院名称" prop="hospitalName">
        <el-input v-model="formData.hospitalName" placeholder="请输入医院名称"  disabled/>
      </el-form-item>
      <el-form-item label="科室名称" prop="sectionName">
        <el-input v-model="formData.sectionName" placeholder="请输入科室名称" disabled />
      </el-form-item>
      <el-form-item label="期望就诊时间" prop="hopeTime">
        <el-date-picker
          v-model="formData.hopeTime"
          type="datetime"
          value-format="x"
          placeholder="选择期望就诊时间"
          disabled
        />
      </el-form-item>
      <el-form-item label="额外需求" prop="memberRemark">
        <el-input v-model="formData.memberRemark"  disabled />
      </el-form-item>
      <el-form-item label="服务项目" prop="programName">
        <el-input v-model="formData.programName" placeholder="请输入服务项目名称" disabled/>
      </el-form-item>
      <el-form-item label="就诊人" prop="paitentName">
        <el-input v-model="formData.paitentName" placeholder="请输入就诊人姓名" disabled/>
      </el-form-item>
      <el-form-item label="服务人员" prop="staffName">
        <el-input v-model="formData.staffName" placeholder="请输入服务人员姓名" disabled/>
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-input :value="formData.payStatus==0?'未支付':'已支付'"  disabled />
      </el-form-item>
      <el-form-item label="订单支付时间" prop="payTime">
        <el-date-picker
          v-model="formData.payTime"
          type="datetime"
          value-format="x"
          disabled
        />
      </el-form-item>
      <el-form-item label="订单完成时间" prop="finishTime">
        <el-date-picker
          v-model="formData.finishTime"
          type="datetime"
          value-format="x"
         disabled
        />
      </el-form-item>
      <el-form-item label="订单取消时间" prop="cancelTime">
        <el-date-picker
          v-model="formData.cancelTime"
          type="datetime"
          value-format="x"
         disabled
        />
      </el-form-item>
      <el-form-item label="商品原价(元)" prop="totalPrice" label-width="110px">
        <el-input :value="(formData.totalPrice/100).toFixed(2)" disabled/>
      </el-form-item>
      <el-form-item label="订单优惠(元)" prop="discountPrice" label-width="110px">
        <el-input :value="(formData.discountPrice/100).toFixed(2)"  disabled/>
      </el-form-item>
      <el-form-item label="应付金额(元)" prop="payPrice" label-width="110px">
        <el-input :value="(formData.payPrice/100).toFixed(2)"  disabled/>
      </el-form-item>
      <el-form-item label="退款金额(元)" prop="refundPrice" label-width="110px">
        <el-input :value="(formData.refundPrice/100).toFixed(2)"  disabled/>
      </el-form-item>
      <el-form-item label="使用的积分" prop="usePoint" label-width="110px">
        <el-input v-model="formData.usePoint" placeholder="请输入使用的积分" disabled/>
      </el-form-item>
      <el-form-item label="积分抵扣的金额" prop="pointPrice" label-width="130px">
        <el-input :value="(formData.pointPrice/100).toFixed(2)"  disabled/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">关闭</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { BuiessOrderApi, BuiessOrderVO } from '@/api/peizhen/buiessorder'

/** 订单 表单 */
defineOptions({ name: 'BuiessOrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  no: undefined,
  hospitalId: undefined,
  hospitalName: undefined,
  sectionId: undefined,
  sectionName: undefined,
  hopeTime: undefined,
  type: undefined,
  memberId: undefined,
  memberRemark: undefined,
  programId: undefined,
  programName: undefined,
  paitentId: undefined,
  paitentName: undefined,
  staffId: undefined,
  staffName: undefined,
  status: undefined,
  remark: undefined,
  payOrderId: undefined,
  payStatus: undefined,
  payTime: undefined,
  payChannelCode: undefined,
  finishTime: undefined,
  cancelTime: undefined,
  totalPrice: undefined,
  discountPrice: undefined,
  payPrice: undefined,
  refundPrice: undefined,
  usePoint: undefined,
  pointPrice: undefined,
})

const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '订单信息'
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await BuiessOrderApi.getBuiessOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    no: undefined,
    hospitalId: undefined,
    hospitalName: undefined,
    sectionId: undefined,
    sectionName: undefined,
    hopeTime: undefined,
    type: undefined,
    memberId: undefined,
    memberRemark: undefined,
    programId: undefined,
    programName: undefined,
    paitentId: undefined,
    paitentName: undefined,
    staffId: undefined,
    staffName: undefined,
    status: undefined,
    remark: undefined,
    payOrderId: undefined,
    payStatus: undefined,
    payTime: undefined,
    payChannelCode: undefined,
    finishTime: undefined,
    cancelTime: undefined,
    totalPrice: undefined,
    discountPrice: undefined,
    payPrice: undefined,
    refundPrice: undefined,
    usePoint: undefined,
    pointPrice: undefined,
  }
  formRef.value?.resetFields()
}
</script>
